<template>
  <div>
    <!-- 面包屑导航 -->
    <Breadcrumb />
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="reSearch"
          ></el-input>
        </el-col>
        <el-col :span="4"
          ><el-button type="primary" icon="el-icon-search" @click="toSearch"
            >搜索</el-button
          ></el-col
        >
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="orderList" border stripe>
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="80"
        ></el-table-column>
        <el-table-column
          label="订单编号"
          align="center"
          prop="order_number"
        ></el-table-column>
        <el-table-column
          label="订单价格"
          align="center"
          prop="order_price"
        ></el-table-column>
        <el-table-column label="是否付款" align="center" prop="pay_status">
          <template slot-scope="{ row }">
            <el-tag v-if="row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="是否发货"
          prop="is_send"
        ></el-table-column>
        <el-table-column align="center" label="下单时间">
          <template slot-scope="{ row }">
            {{ row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template>
            <el-tooltip
              effect="dark"
              content="修改地址"
              placement="top-end"
              :enterable="false"
            >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="showBox"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="查看物流信息"
              placement="top-end"
              :enterable="false"
            >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-location"
                @click="showLogistics"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 7, 10]"
        :page-size="queryInfo.pagesize"
        layout="total,sizes, prev, pager, next,jumper"
        :total="total"
        background
        style="text-align: center; margin-top: 20px"
      >
      </el-pagination>
      <!-- 修改对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="editDialogVisible"
        @closed="addressDiglogClose"
        width="50%"
      >
        <el-form
          :model="addressInfo"
          :rules="addressFormRules"
          ref="addressFormRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              v-model="addressInfo.address1"
              :options="Citydata"
              :props="set"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input
              v-model="addressInfo.address2"
              placeholder="请输入详细地址"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editDialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 展示物流进度对话框 -->
      <el-dialog
        title="物流进度"
        :visible.sync="logisticsDiglogVisible"
        width="50%"
      >
        <el-timeline :reverse="true">
          <el-timeline-item
            v-for="activity in logisticsInfo"
            :key="activity.time"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import Citydata from "./citydata.js";
export default {
  name: "Order",
  data() {
    return {
      orderList: [],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
        user_id: "",
        pay_status: "",
        is_send: "",
      },
      total: 0,
      //控制修改对话框显示隐藏
      editDialogVisible: false,
      //查看物流
      logisticsDiglogVisible: false,
      //物流信息
      logisticsInfo: [],
      addressInfo: {
        address1: [],
        address2: "",
      },
      //验证规则
      addressFormRules: {
        address1: [
          { required: true, message: "请选择地址", trigger: "change" },
        ],
        address2: [
          { required: true, message: "请填写详细地址", trigger: "change" },
        ],
      },
      //级联选择器配置
      set: {
        expandTrigger: "hover",
        value: "value",
        label: "label",
        children: "children",
      },
      //城市列表
      Citydata,
    };
  },
  async created() {
    this.getOrderList();
    const res = await this.$http.Login({
      username: "admin",
      password: "123456",
    });
    if (res.meta.status === 200) {
      //保存token
      window.sessionStorage.setItem("token1", res.data.token);
    }
  },
  methods: {
    //获取商品订单列表
    async getOrderList() {
      const res = await this.$http.getOrderList({ params: this.queryInfo });
      if (res.meta.status !== 200) {
        return this.$message.error("获取订单列表失败");
      }
      this.orderList = res.data.goods;
      this.total = res.data.total;
    },
    //点击input清除
    reSearch() {
      this.getOrderList();
    },
    //点击搜索按钮
    toSearch() {
      this.getOrderList();
    },
    //页面展示数量改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    //页码改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrderList();
    },
    //显示修改对话框
    showBox() {
      this.editDialogVisible = true;
    },
    //级联选择器改变
    handleChange() {},
    //修改对话框关闭
    addressDiglogClose() {
      this.$refs.addressFormRef.resetFields();
    },
    //打开查看物流
    async showLogistics() {
      const resp = await this.$http.getLogistics("1106975712662");
      if (resp.meta.status !== 200) {
        return this.$message.error("获取物流信息失败");
      }
      this.logisticsInfo = resp.data;
      this.logisticsDiglogVisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
